
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.analysis.elasticity.elastic &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script src="../../../../_static/jquery.js"></script>
    <script src="../../../../_static/underscore.js"></script>
    <script src="../../../../_static/doctools.js"></script>
    <script src="../../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.elasticity.elastic</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.analysis.elasticity.elastic</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>


<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">This module provides a class used to describe the elastic tensor,</span>
<span class="sd">including methods used to fit the elastic tensor from linear response</span>
<span class="sd">stress-strain data</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">pymatgen.core.tensors</span> <span class="kn">import</span> <span class="n">Tensor</span><span class="p">,</span> \
    <span class="n">TensorCollection</span><span class="p">,</span> <span class="n">get_uvec</span><span class="p">,</span> <span class="n">SquareTensor</span><span class="p">,</span> <span class="n">DEFAULT_QUAD</span>
<span class="kn">from</span> <span class="nn">pymatgen.analysis.elasticity.stress</span> <span class="kn">import</span> <span class="n">Stress</span>
<span class="kn">from</span> <span class="nn">pymatgen.analysis.elasticity.strain</span> <span class="kn">import</span> <span class="n">Strain</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.units</span> <span class="kn">import</span> <span class="n">Unit</span>
<span class="kn">from</span> <span class="nn">scipy.special</span> <span class="kn">import</span> <span class="n">factorial</span>
<span class="kn">from</span> <span class="nn">scipy.integrate</span> <span class="kn">import</span> <span class="n">quad</span>
<span class="kn">from</span> <span class="nn">scipy.optimize</span> <span class="kn">import</span> <span class="n">root</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">OrderedDict</span>
<span class="kn">from</span> <span class="nn">monty.dev</span> <span class="kn">import</span> <span class="n">deprecated</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">import</span> <span class="nn">itertools</span>

<span class="kn">import</span> <span class="nn">sympy</span> <span class="k">as</span> <span class="nn">sp</span>

<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Joseph Montoya&quot;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot;Copyright 2012, The Materials Project&quot;</span>
<span class="n">__credits__</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;Maarten de Jong, Ian Winter, Shyam Dwaraknath, &quot;</span>
               <span class="s2">&quot;Mark Asta, Anubhav Jain&quot;</span><span class="p">)</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s2">&quot;1.0&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Joseph Montoya&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;montoyjh@lbl.gov&quot;</span>
<span class="n">__status__</span> <span class="o">=</span> <span class="s2">&quot;Production&quot;</span>
<span class="n">__date__</span> <span class="o">=</span> <span class="s2">&quot;July 24, 2018&quot;</span>


<div class="viewcode-block" id="NthOrderElasticTensor"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.NthOrderElasticTensor">[docs]</a><span class="k">class</span> <span class="nc">NthOrderElasticTensor</span><span class="p">(</span><span class="n">Tensor</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    An object representing an nth-order tensor expansion</span>
<span class="sd">    of the stress-strain constitutive equations</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">GPa_to_eV_A3</span> <span class="o">=</span> <span class="n">Unit</span><span class="p">(</span><span class="s2">&quot;GPa&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">get_conversion_factor</span><span class="p">(</span><span class="n">Unit</span><span class="p">(</span><span class="s2">&quot;eV ang^-3&quot;</span><span class="p">))</span>
    <span class="n">symbol</span> <span class="o">=</span> <span class="s2">&quot;C&quot;</span>

    <span class="k">def</span> <span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">input_array</span><span class="p">,</span> <span class="n">check_rank</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            input_array ():</span>
<span class="sd">            check_rank ():</span>
<span class="sd">            tol ():</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span>
            <span class="bp">cls</span><span class="p">,</span> <span class="n">input_array</span><span class="p">,</span> <span class="n">check_rank</span><span class="o">=</span><span class="n">check_rank</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">obj</span><span class="o">.</span><span class="n">rank</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;ElasticTensor must have even rank&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">obj</span><span class="o">.</span><span class="n">is_voigt_symmetric</span><span class="p">(</span><span class="n">tol</span><span class="p">):</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;Input elastic tensor does not satisfy &quot;</span>
                          <span class="s2">&quot;standard voigt symmetries&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="bp">cls</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">order</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Order of the elastic tensor</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">rank</span> <span class="o">//</span> <span class="mi">2</span>

<div class="viewcode-block" id="NthOrderElasticTensor.calculate_stress"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.NthOrderElasticTensor.calculate_stress">[docs]</a>    <span class="k">def</span> <span class="nf">calculate_stress</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">strain</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculate&#39;s a given elastic tensor&#39;s contribution to the</span>
<span class="sd">        stress using Einstein summation</span>

<span class="sd">        Args:</span>
<span class="sd">            strain (3x3 array-like): matrix corresponding to strain</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">strain</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">strain</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">strain</span><span class="o">.</span><span class="n">shape</span> <span class="o">==</span> <span class="p">(</span><span class="mi">6</span><span class="p">,):</span>
            <span class="n">strain</span> <span class="o">=</span> <span class="n">Strain</span><span class="o">.</span><span class="n">from_voigt</span><span class="p">(</span><span class="n">strain</span><span class="p">)</span>
        <span class="k">assert</span> <span class="n">strain</span><span class="o">.</span><span class="n">shape</span> <span class="o">==</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="s2">&quot;Strain must be 3x3 or voigt-notation&quot;</span>
        <span class="n">stress_matrix</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">einsum_sequence</span><span class="p">([</span><span class="n">strain</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span> <span class="o">/</span> <span class="n">factorial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">Stress</span><span class="p">(</span><span class="n">stress_matrix</span><span class="p">)</span></div>

<div class="viewcode-block" id="NthOrderElasticTensor.energy_density"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.NthOrderElasticTensor.energy_density">[docs]</a>    <span class="k">def</span> <span class="nf">energy_density</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">strain</span><span class="p">,</span> <span class="n">convert_GPa_to_eV</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates the elastic energy density due to a strain</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">e_density</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">calculate_stress</span><span class="p">(</span><span class="n">strain</span><span class="p">)</span> <span class="o">*</span> <span class="n">strain</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span>
        <span class="k">if</span> <span class="n">convert_GPa_to_eV</span><span class="p">:</span>
            <span class="n">e_density</span> <span class="o">*=</span> <span class="bp">self</span><span class="o">.</span><span class="n">GPa_to_eV_A3</span>  <span class="c1"># Conversion factor for GPa to eV/A^3</span>
        <span class="k">return</span> <span class="n">e_density</span></div>

<div class="viewcode-block" id="NthOrderElasticTensor.from_diff_fit"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.NthOrderElasticTensor.from_diff_fit">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_diff_fit</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">strains</span><span class="p">,</span> <span class="n">stresses</span><span class="p">,</span> <span class="n">eq_stress</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                      <span class="n">order</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">1e-10</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>

<span class="sd">        Args:</span>
<span class="sd">            strains ():</span>
<span class="sd">            stresses ():</span>
<span class="sd">            eq_stress ():</span>
<span class="sd">            order ():</span>
<span class="sd">            tol ():</span>

<span class="sd">        Returns:</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">diff_fit</span><span class="p">(</span><span class="n">strains</span><span class="p">,</span> <span class="n">stresses</span><span class="p">,</span> <span class="n">eq_stress</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">tol</span><span class="p">)[</span><span class="n">order</span> <span class="o">-</span> <span class="mi">2</span><span class="p">])</span></div></div>


<div class="viewcode-block" id="raise_error_if_unphysical"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.raise_error_if_unphysical">[docs]</a><span class="k">def</span> <span class="nf">raise_error_if_unphysical</span><span class="p">(</span><span class="n">f</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Wrapper for functions or properties that should raise an error</span>
<span class="sd">    if tensor is unphysical.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            self ():</span>
<span class="sd">            *args ():</span>
<span class="sd">            **kwargs ():</span>

<span class="sd">        Returns:</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_vrh</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">g_vrh</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Bulk or shear modulus is negative, property &quot;</span>
                             <span class="s2">&quot;cannot be determined&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">f</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">wrapper</span></div>


<div class="viewcode-block" id="ElasticTensor"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensor">[docs]</a><span class="k">class</span> <span class="nc">ElasticTensor</span><span class="p">(</span><span class="n">NthOrderElasticTensor</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This class extends Tensor to describe the 3x3x3x3</span>
<span class="sd">    second-order elastic tensor, C_{ijkl}, with various</span>
<span class="sd">    methods for estimating other properties derived from</span>
<span class="sd">    the second order elastic tensor</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">input_array</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Create an ElasticTensor object.  The constructor throws an error if</span>
<span class="sd">        the shape of the input_matrix argument is not 3x3x3x3, i. e. in true</span>
<span class="sd">        tensor notation.  Issues a warning if the input_matrix argument does</span>
<span class="sd">        not satisfy standard symmetries.  Note that the constructor uses</span>
<span class="sd">        __new__ rather than __init__ according to the standard method of</span>
<span class="sd">        subclassing numpy ndarrays.</span>

<span class="sd">        Args:</span>
<span class="sd">            input_array (3x3x3x3 array-like): the 3x3x3x3 array-like</span>
<span class="sd">                representing the elastic tensor</span>

<span class="sd">            tol (float): tolerance for initial symmetry test of tensor</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">obj</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">input_array</span><span class="p">,</span>
                              <span class="n">check_rank</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="n">tol</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="bp">cls</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">compliance_tensor</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        returns the Voigt-notation compliance tensor,</span>
<span class="sd">        which is the matrix inverse of the</span>
<span class="sd">        Voigt-notation elastic tensor</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">s_voigt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">inv</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">voigt</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ComplianceTensor</span><span class="o">.</span><span class="n">from_voigt</span><span class="p">(</span><span class="n">s_voigt</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">k_voigt</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        returns the K_v bulk modulus</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">voigt</span><span class="p">[:</span><span class="mi">3</span><span class="p">,</span> <span class="p">:</span><span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">g_voigt</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        returns the G_v shear modulus</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span><span class="mf">2.</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">voigt</span><span class="p">[:</span><span class="mi">3</span><span class="p">,</span> <span class="p">:</span><span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">trace</span><span class="p">()</span> <span class="o">-</span>
                <span class="n">np</span><span class="o">.</span><span class="n">triu</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">voigt</span><span class="p">[:</span><span class="mi">3</span><span class="p">,</span> <span class="p">:</span><span class="mi">3</span><span class="p">])</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span> <span class="o">+</span>
                <span class="mi">3</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">voigt</span><span class="p">[</span><span class="mi">3</span><span class="p">:,</span> <span class="mi">3</span><span class="p">:]</span><span class="o">.</span><span class="n">trace</span><span class="p">())</span> <span class="o">/</span> <span class="mf">15.</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">k_reuss</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        returns the K_r bulk modulus</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="mf">1.</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">compliance_tensor</span><span class="o">.</span><span class="n">voigt</span><span class="p">[:</span><span class="mi">3</span><span class="p">,</span> <span class="p">:</span><span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">g_reuss</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        returns the G_r shear modulus</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="mf">15.</span> <span class="o">/</span> <span class="p">(</span><span class="mf">8.</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">compliance_tensor</span><span class="o">.</span><span class="n">voigt</span><span class="p">[:</span><span class="mi">3</span><span class="p">,</span> <span class="p">:</span><span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">trace</span><span class="p">()</span> <span class="o">-</span>
                      <span class="mf">4.</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">triu</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">compliance_tensor</span><span class="o">.</span><span class="n">voigt</span><span class="p">[:</span><span class="mi">3</span><span class="p">,</span> <span class="p">:</span><span class="mi">3</span><span class="p">])</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span> <span class="o">+</span>
                      <span class="mf">3.</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">compliance_tensor</span><span class="o">.</span><span class="n">voigt</span><span class="p">[</span><span class="mi">3</span><span class="p">:,</span> <span class="mi">3</span><span class="p">:]</span><span class="o">.</span><span class="n">trace</span><span class="p">())</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">k_vrh</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        returns the K_vrh (Voigt-Reuss-Hill) average bulk modulus</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_voigt</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_reuss</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">g_vrh</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        returns the G_vrh (Voigt-Reuss-Hill) average shear modulus</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">g_voigt</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">g_reuss</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">y_mod</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates Young&#39;s modulus (in SI units) using the</span>
<span class="sd">        Voigt-Reuss-Hill averages of bulk and shear moduli</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="mf">9.e9</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_vrh</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">g_vrh</span> <span class="o">/</span> <span class="p">(</span><span class="mf">3.</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_vrh</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">g_vrh</span><span class="p">)</span>

<div class="viewcode-block" id="ElasticTensor.directional_poisson_ratio"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensor.directional_poisson_ratio">[docs]</a>    <span class="k">def</span> <span class="nf">directional_poisson_ratio</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">1e-8</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates the poisson ratio for a specific direction</span>
<span class="sd">        relative to a second, orthogonal direction</span>

<span class="sd">        Args:</span>
<span class="sd">            n (3-d vector): principal direction</span>
<span class="sd">            m (3-d vector): secondary direction orthogonal to n</span>
<span class="sd">            tol (float): tolerance for testing of orthogonality</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">n</span><span class="p">,</span> <span class="n">m</span> <span class="o">=</span> <span class="n">get_uvec</span><span class="p">(</span><span class="n">n</span><span class="p">),</span> <span class="n">get_uvec</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">))</span> <span class="o">&lt;</span> <span class="n">tol</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;n and m must be orthogonal&quot;</span><span class="p">)</span>
        <span class="n">v</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">compliance_tensor</span><span class="o">.</span><span class="n">einsum_sequence</span><span class="p">([</span><span class="n">n</span><span class="p">]</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="p">[</span><span class="n">m</span><span class="p">]</span> <span class="o">*</span> <span class="mi">2</span><span class="p">)</span>
        <span class="n">v</span> <span class="o">*=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">compliance_tensor</span><span class="o">.</span><span class="n">einsum_sequence</span><span class="p">([</span><span class="n">n</span><span class="p">]</span> <span class="o">*</span> <span class="mi">4</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">v</span></div>

<div class="viewcode-block" id="ElasticTensor.directional_elastic_mod"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensor.directional_elastic_mod">[docs]</a>    <span class="k">def</span> <span class="nf">directional_elastic_mod</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates directional elastic modulus for a specific vector</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">n</span> <span class="o">=</span> <span class="n">get_uvec</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">einsum_sequence</span><span class="p">([</span><span class="n">n</span><span class="p">]</span> <span class="o">*</span> <span class="mi">4</span><span class="p">)</span></div>

<div class="viewcode-block" id="ElasticTensor.trans_v"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensor.trans_v">[docs]</a>    <span class="nd">@raise_error_if_unphysical</span>
    <span class="k">def</span> <span class="nf">trans_v</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates transverse sound velocity (in SI units) using the</span>
<span class="sd">        Voigt-Reuss-Hill average bulk modulus</span>

<span class="sd">        Args:</span>
<span class="sd">            structure: pymatgen structure object</span>

<span class="sd">        Returns: transverse sound velocity (in SI units)</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">nsites</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">num_sites</span>
        <span class="n">volume</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">volume</span>
        <span class="n">natoms</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">num_atoms</span>
        <span class="n">weight</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">weight</span><span class="p">)</span>
        <span class="n">mass_density</span> <span class="o">=</span> <span class="mf">1.6605e3</span> <span class="o">*</span> <span class="n">nsites</span> <span class="o">*</span> <span class="n">weight</span> <span class="o">/</span> <span class="p">(</span><span class="n">natoms</span> <span class="o">*</span> <span class="n">volume</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">g_vrh</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;k_vrh or g_vrh is negative, &quot;</span>
                             <span class="s2">&quot;sound velocity is undefined&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">(</span><span class="mf">1e9</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">g_vrh</span> <span class="o">/</span> <span class="n">mass_density</span><span class="p">)</span> <span class="o">**</span> <span class="mf">0.5</span></div>

<div class="viewcode-block" id="ElasticTensor.long_v"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensor.long_v">[docs]</a>    <span class="nd">@raise_error_if_unphysical</span>
    <span class="k">def</span> <span class="nf">long_v</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates longitudinal sound velocity (in SI units)</span>
<span class="sd">        using the Voigt-Reuss-Hill average bulk modulus</span>

<span class="sd">        Args:</span>
<span class="sd">            structure: pymatgen structure object</span>

<span class="sd">        Returns: longitudinal sound velocity (in SI units)</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">nsites</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">num_sites</span>
        <span class="n">volume</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">volume</span>
        <span class="n">natoms</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">num_atoms</span>
        <span class="n">weight</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">weight</span><span class="p">)</span>
        <span class="n">mass_density</span> <span class="o">=</span> <span class="mf">1.6605e3</span> <span class="o">*</span> <span class="n">nsites</span> <span class="o">*</span> <span class="n">weight</span> <span class="o">/</span> <span class="p">(</span><span class="n">natoms</span> <span class="o">*</span> <span class="n">volume</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">g_vrh</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;k_vrh or g_vrh is negative, &quot;</span>
                             <span class="s2">&quot;sound velocity is undefined&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">(</span><span class="mf">1e9</span> <span class="o">*</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_vrh</span> <span class="o">+</span> <span class="mf">4.</span> <span class="o">/</span> <span class="mf">3.</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">g_vrh</span><span class="p">)</span> <span class="o">/</span> <span class="n">mass_density</span><span class="p">)</span> <span class="o">**</span> <span class="mf">0.5</span></div>

<div class="viewcode-block" id="ElasticTensor.snyder_ac"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensor.snyder_ac">[docs]</a>    <span class="nd">@raise_error_if_unphysical</span>
    <span class="k">def</span> <span class="nf">snyder_ac</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates Snyder&#39;s acoustic sound velocity (in SI units)</span>

<span class="sd">        Args:</span>
<span class="sd">            structure: pymatgen structure object</span>

<span class="sd">        Returns: Snyder&#39;s acoustic sound velocity (in SI units)</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">nsites</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">num_sites</span>
        <span class="n">volume</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">volume</span>
        <span class="n">natoms</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">num_atoms</span>
        <span class="n">num_density</span> <span class="o">=</span> <span class="mf">1e30</span> <span class="o">*</span> <span class="n">nsites</span> <span class="o">/</span> <span class="n">volume</span>
        <span class="n">tot_mass</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">e</span><span class="o">.</span><span class="n">atomic_mass</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">structure</span><span class="o">.</span><span class="n">species</span><span class="p">])</span>
        <span class="n">avg_mass</span> <span class="o">=</span> <span class="mf">1.6605e-27</span> <span class="o">*</span> <span class="n">tot_mass</span> <span class="o">/</span> <span class="n">natoms</span>
        <span class="k">return</span> <span class="mf">0.38483</span> <span class="o">*</span> <span class="n">avg_mass</span> <span class="o">*</span> <span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">long_v</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span> <span class="o">+</span> <span class="mf">2.</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">trans_v</span><span class="p">(</span><span class="n">structure</span><span class="p">))</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">)</span> <span class="o">**</span> <span class="mf">3.</span> \
            <span class="o">/</span> <span class="p">(</span><span class="mf">300.</span> <span class="o">*</span> <span class="n">num_density</span> <span class="o">**</span> <span class="p">(</span><span class="o">-</span><span class="mf">2.</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">)</span> <span class="o">*</span> <span class="n">nsites</span> <span class="o">**</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">))</span></div>

<div class="viewcode-block" id="ElasticTensor.snyder_opt"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensor.snyder_opt">[docs]</a>    <span class="nd">@raise_error_if_unphysical</span>
    <span class="k">def</span> <span class="nf">snyder_opt</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates Snyder&#39;s optical sound velocity (in SI units)</span>

<span class="sd">        Args:</span>
<span class="sd">            structure: pymatgen structure object</span>

<span class="sd">        Returns: Snyder&#39;s optical sound velocity (in SI units)</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">nsites</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">num_sites</span>
        <span class="n">volume</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">volume</span>
        <span class="n">num_density</span> <span class="o">=</span> <span class="mf">1e30</span> <span class="o">*</span> <span class="n">nsites</span> <span class="o">/</span> <span class="n">volume</span>
        <span class="k">return</span> <span class="mf">1.66914e-23</span> <span class="o">*</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">long_v</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span> <span class="o">+</span> <span class="mf">2.</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">trans_v</span><span class="p">(</span><span class="n">structure</span><span class="p">))</span> <span class="o">/</span> <span class="mf">3.</span> \
            <span class="o">/</span> <span class="n">num_density</span> <span class="o">**</span> <span class="p">(</span><span class="o">-</span><span class="mf">2.</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">nsites</span> <span class="o">**</span> <span class="p">(</span><span class="o">-</span><span class="mf">1.</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">))</span></div>

<div class="viewcode-block" id="ElasticTensor.snyder_total"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensor.snyder_total">[docs]</a>    <span class="nd">@raise_error_if_unphysical</span>
    <span class="k">def</span> <span class="nf">snyder_total</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates Snyder&#39;s total sound velocity (in SI units)</span>

<span class="sd">        Args:</span>
<span class="sd">            structure: pymatgen structure object</span>

<span class="sd">        Returns: Snyder&#39;s total sound velocity (in SI units)</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">snyder_ac</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">snyder_opt</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span></div>

<div class="viewcode-block" id="ElasticTensor.clarke_thermalcond"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensor.clarke_thermalcond">[docs]</a>    <span class="nd">@raise_error_if_unphysical</span>
    <span class="k">def</span> <span class="nf">clarke_thermalcond</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates Clarke&#39;s thermal conductivity (in SI units)</span>

<span class="sd">        Args:</span>
<span class="sd">            structure: pymatgen structure object</span>

<span class="sd">        Returns: Clarke&#39;s thermal conductivity (in SI units)</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">nsites</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">num_sites</span>
        <span class="n">volume</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">volume</span>
        <span class="n">tot_mass</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">e</span><span class="o">.</span><span class="n">atomic_mass</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">structure</span><span class="o">.</span><span class="n">species</span><span class="p">])</span>
        <span class="n">natoms</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">num_atoms</span>
        <span class="n">weight</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">weight</span><span class="p">)</span>
        <span class="n">avg_mass</span> <span class="o">=</span> <span class="mf">1.6605e-27</span> <span class="o">*</span> <span class="n">tot_mass</span> <span class="o">/</span> <span class="n">natoms</span>
        <span class="n">mass_density</span> <span class="o">=</span> <span class="mf">1.6605e3</span> <span class="o">*</span> <span class="n">nsites</span> <span class="o">*</span> <span class="n">weight</span> <span class="o">/</span> <span class="p">(</span><span class="n">natoms</span> <span class="o">*</span> <span class="n">volume</span><span class="p">)</span>
        <span class="k">return</span> <span class="mf">0.87</span> <span class="o">*</span> <span class="mf">1.3806e-23</span> <span class="o">*</span> <span class="n">avg_mass</span> <span class="o">**</span> <span class="p">(</span><span class="o">-</span><span class="mf">2.</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">)</span> <span class="o">*</span> <span class="n">mass_density</span> <span class="o">**</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">/</span> <span class="mf">6.</span><span class="p">)</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_mod</span> <span class="o">**</span> <span class="mf">0.5</span></div>

<div class="viewcode-block" id="ElasticTensor.cahill_thermalcond"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensor.cahill_thermalcond">[docs]</a>    <span class="nd">@raise_error_if_unphysical</span>
    <span class="k">def</span> <span class="nf">cahill_thermalcond</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates Cahill&#39;s thermal conductivity (in SI units)</span>

<span class="sd">        Args:</span>
<span class="sd">            structure: pymatgen structure object</span>

<span class="sd">        Returns: Cahill&#39;s thermal conductivity (in SI units)</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">nsites</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">num_sites</span>
        <span class="n">volume</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">volume</span>
        <span class="n">num_density</span> <span class="o">=</span> <span class="mf">1e30</span> <span class="o">*</span> <span class="n">nsites</span> <span class="o">/</span> <span class="n">volume</span>
        <span class="k">return</span> <span class="mf">1.3806e-23</span> <span class="o">/</span> <span class="mf">2.48</span> <span class="o">*</span> <span class="n">num_density</span> <span class="o">**</span> <span class="p">(</span><span class="mf">2.</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">long_v</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">trans_v</span><span class="p">(</span><span class="n">structure</span><span class="p">))</span></div>

<div class="viewcode-block" id="ElasticTensor.debye_temperature"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensor.debye_temperature">[docs]</a>    <span class="nd">@raise_error_if_unphysical</span>
    <span class="k">def</span> <span class="nf">debye_temperature</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Estimates the debye temperature from longitudinal and</span>
<span class="sd">        transverse sound velocities</span>

<span class="sd">        Args:</span>
<span class="sd">            structure: pymatgen structure object</span>

<span class="sd">        Returns: debye temperature (in SI units)</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">v0</span> <span class="o">=</span> <span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">volume</span> <span class="o">*</span> <span class="mf">1e-30</span> <span class="o">/</span> <span class="n">structure</span><span class="o">.</span><span class="n">num_sites</span><span class="p">)</span>
        <span class="n">vl</span><span class="p">,</span> <span class="n">vt</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">long_v</span><span class="p">(</span><span class="n">structure</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">trans_v</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>
        <span class="n">vm</span> <span class="o">=</span> <span class="mi">3</span> <span class="o">**</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="n">vl</span> <span class="o">**</span> <span class="mi">3</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span> <span class="n">vt</span> <span class="o">**</span> <span class="mi">3</span><span class="p">)</span> <span class="o">**</span> <span class="p">(</span><span class="o">-</span><span class="mf">1.</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">)</span>
        <span class="n">td</span> <span class="o">=</span> <span class="mf">1.05457e-34</span> <span class="o">/</span> <span class="mf">1.38065e-23</span> <span class="o">*</span> <span class="n">vm</span> <span class="o">*</span> <span class="p">(</span><span class="mi">6</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">/</span> <span class="n">v0</span><span class="p">)</span> <span class="o">**</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">/</span> <span class="mf">3.</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">td</span></div>

    <span class="nd">@deprecated</span><span class="p">(</span><span class="s2">&quot;debye_temperature_from_sound_velocities is now the default&quot;</span>
                <span class="s2">&quot;debye_temperature function, this one will be removed.&quot;</span><span class="p">)</span>
    <span class="nd">@raise_error_if_unphysical</span>
    <span class="k">def</span> <span class="nf">debye_temperature_from_sound_velocities</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Estimates Debye temperature from sound velocities</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">debye_temperature</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">universal_anisotropy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        returns the universal anisotropy value</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="mf">5.</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">g_voigt</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">g_reuss</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_voigt</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_reuss</span> <span class="o">-</span> <span class="mf">6.</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">homogeneous_poisson</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        returns the homogeneous poisson ratio</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">-</span> <span class="mf">2.</span> <span class="o">/</span> <span class="mf">3.</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">g_vrh</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_vrh</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="mf">2.</span> <span class="o">+</span> <span class="mf">2.</span> <span class="o">/</span> <span class="mf">3.</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">g_vrh</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">k_vrh</span><span class="p">)</span>

<div class="viewcode-block" id="ElasticTensor.green_kristoffel"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensor.green_kristoffel">[docs]</a>    <span class="k">def</span> <span class="nf">green_kristoffel</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">u</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the Green-Kristoffel tensor for a second-order tensor</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">einsum_sequence</span><span class="p">([</span><span class="n">u</span><span class="p">,</span> <span class="n">u</span><span class="p">],</span> <span class="s2">&quot;ijkl,i,l&quot;</span><span class="p">)</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">property_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        returns a dictionary of properties derived from the elastic tensor</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">props</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;k_voigt&quot;</span><span class="p">,</span> <span class="s2">&quot;k_reuss&quot;</span><span class="p">,</span> <span class="s2">&quot;k_vrh&quot;</span><span class="p">,</span> <span class="s2">&quot;g_voigt&quot;</span><span class="p">,</span> <span class="s2">&quot;g_reuss&quot;</span><span class="p">,</span> <span class="s2">&quot;g_vrh&quot;</span><span class="p">,</span>
                 <span class="s2">&quot;universal_anisotropy&quot;</span><span class="p">,</span> <span class="s2">&quot;homogeneous_poisson&quot;</span><span class="p">,</span> <span class="s2">&quot;y_mod&quot;</span><span class="p">]</span>
        <span class="k">return</span> <span class="p">{</span><span class="n">prop</span><span class="p">:</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prop</span><span class="p">)</span> <span class="k">for</span> <span class="n">prop</span> <span class="ow">in</span> <span class="n">props</span><span class="p">}</span>

<div class="viewcode-block" id="ElasticTensor.get_structure_property_dict"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensor.get_structure_property_dict">[docs]</a>    <span class="k">def</span> <span class="nf">get_structure_property_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">include_base_props</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                                    <span class="n">ignore_errors</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        returns a dictionary of properties derived from the elastic tensor</span>
<span class="sd">        and an associated structure</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): structure object for which to calculate</span>
<span class="sd">                associated properties</span>
<span class="sd">            include_base_props (bool): whether to include base properties,</span>
<span class="sd">                like k_vrh, etc.</span>
<span class="sd">            ignore_errors (bool): if set to true, will set problem properties</span>
<span class="sd">                that depend on a physical tensor to None, defaults to False</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">s_props</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;trans_v&quot;</span><span class="p">,</span> <span class="s2">&quot;long_v&quot;</span><span class="p">,</span> <span class="s2">&quot;snyder_ac&quot;</span><span class="p">,</span> <span class="s2">&quot;snyder_opt&quot;</span><span class="p">,</span>
                   <span class="s2">&quot;snyder_total&quot;</span><span class="p">,</span> <span class="s2">&quot;clarke_thermalcond&quot;</span><span class="p">,</span> <span class="s2">&quot;cahill_thermalcond&quot;</span><span class="p">,</span>
                   <span class="s2">&quot;debye_temperature&quot;</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">ignore_errors</span> <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">k_vrh</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">g_vrh</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">):</span>
            <span class="n">sp_dict</span> <span class="o">=</span> <span class="p">{</span><span class="n">prop</span><span class="p">:</span> <span class="kc">None</span> <span class="k">for</span> <span class="n">prop</span> <span class="ow">in</span> <span class="n">s_props</span><span class="p">}</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sp_dict</span> <span class="o">=</span> <span class="p">{</span><span class="n">prop</span><span class="p">:</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prop</span><span class="p">)(</span><span class="n">structure</span><span class="p">)</span> <span class="k">for</span> <span class="n">prop</span> <span class="ow">in</span> <span class="n">s_props</span><span class="p">}</span>
        <span class="n">sp_dict</span><span class="p">[</span><span class="s2">&quot;structure&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">structure</span>
        <span class="k">if</span> <span class="n">include_base_props</span><span class="p">:</span>
            <span class="n">sp_dict</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">property_dict</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">sp_dict</span></div>

<div class="viewcode-block" id="ElasticTensor.from_pseudoinverse"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensor.from_pseudoinverse">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_pseudoinverse</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">strains</span><span class="p">,</span> <span class="n">stresses</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Class method to fit an elastic tensor from stress/strain</span>
<span class="sd">        data.  Method uses Moore-Penrose pseudoinverse to invert</span>
<span class="sd">        the s = C*e equation with elastic tensor, stress, and</span>
<span class="sd">        strain in voigt notation</span>

<span class="sd">        Args:</span>
<span class="sd">            stresses (Nx3x3 array-like): list or array of stresses</span>
<span class="sd">            strains (Nx3x3 array-like): list or array of strains</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># convert the stress/strain to Nx6 arrays of voigt-notation</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;Pseudoinverse fitting of Strain/Stress lists may yield &quot;</span>
                      <span class="s2">&quot;questionable results from vasp data, use with caution.&quot;</span><span class="p">)</span>
        <span class="n">stresses</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">Stress</span><span class="p">(</span><span class="n">stress</span><span class="p">)</span><span class="o">.</span><span class="n">voigt</span> <span class="k">for</span> <span class="n">stress</span> <span class="ow">in</span> <span class="n">stresses</span><span class="p">])</span>
        <span class="k">with</span> <span class="n">warnings</span><span class="o">.</span><span class="n">catch_warnings</span><span class="p">(</span><span class="n">record</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
            <span class="n">strains</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">Strain</span><span class="p">(</span><span class="n">strain</span><span class="p">)</span><span class="o">.</span><span class="n">voigt</span> <span class="k">for</span> <span class="n">strain</span> <span class="ow">in</span> <span class="n">strains</span><span class="p">])</span>

        <span class="n">voigt_fit</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">pinv</span><span class="p">(</span><span class="n">strains</span><span class="p">),</span> <span class="n">stresses</span><span class="p">))</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="o">.</span><span class="n">from_voigt</span><span class="p">(</span><span class="n">voigt_fit</span><span class="p">)</span></div>

<div class="viewcode-block" id="ElasticTensor.from_independent_strains"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensor.from_independent_strains">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_independent_strains</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">strains</span><span class="p">,</span> <span class="n">stresses</span><span class="p">,</span> <span class="n">eq_stress</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                                 <span class="n">vasp</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">1e-10</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Constructs the elastic tensor least-squares fit of independent strains</span>
<span class="sd">        Args:</span>
<span class="sd">            strains (list of Strains): list of strain objects to fit</span>
<span class="sd">            stresses (list of Stresses): list of stress objects to use in fit</span>
<span class="sd">                corresponding to the list of strains</span>
<span class="sd">            eq_stress (Stress): equilibrium stress to use in fitting</span>
<span class="sd">            vasp (boolean): flag for whether the stress tensor should be</span>
<span class="sd">                converted based on vasp units/convention for stress</span>
<span class="sd">            tol (float): tolerance for removing near-zero elements of the</span>
<span class="sd">                resulting tensor</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">strain_states</span> <span class="o">=</span> <span class="p">[</span><span class="nb">tuple</span><span class="p">(</span><span class="n">ss</span><span class="p">)</span> <span class="k">for</span> <span class="n">ss</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="mi">6</span><span class="p">)]</span>
        <span class="n">ss_dict</span> <span class="o">=</span> <span class="n">get_strain_state_dict</span><span class="p">(</span><span class="n">strains</span><span class="p">,</span> <span class="n">stresses</span><span class="p">,</span> <span class="n">eq_stress</span><span class="o">=</span><span class="n">eq_stress</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">set</span><span class="p">(</span><span class="n">strain_states</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="nb">set</span><span class="p">(</span><span class="n">ss_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Missing independent strain states: &quot;</span>
                             <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">strain_states</span><span class="p">)</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="n">ss_dict</span><span class="p">)))</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">ss_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="n">strain_states</span><span class="p">))</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;Extra strain states in strain-stress pairs &quot;</span>
                          <span class="s2">&quot;are neglected in independent strain fitting&quot;</span><span class="p">)</span>
        <span class="n">c_ij</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">6</span><span class="p">,</span> <span class="mi">6</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">6</span><span class="p">):</span>
            <span class="n">istrains</span> <span class="o">=</span> <span class="n">ss_dict</span><span class="p">[</span><span class="n">strain_states</span><span class="p">[</span><span class="n">i</span><span class="p">]][</span><span class="s2">&quot;strains&quot;</span><span class="p">]</span>
            <span class="n">istresses</span> <span class="o">=</span> <span class="n">ss_dict</span><span class="p">[</span><span class="n">strain_states</span><span class="p">[</span><span class="n">i</span><span class="p">]][</span><span class="s2">&quot;stresses&quot;</span><span class="p">]</span>
            <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">6</span><span class="p">):</span>
                <span class="n">c_ij</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">polyfit</span><span class="p">(</span><span class="n">istrains</span><span class="p">[:,</span> <span class="n">i</span><span class="p">],</span> <span class="n">istresses</span><span class="p">[:,</span> <span class="n">j</span><span class="p">],</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">vasp</span><span class="p">:</span>
            <span class="n">c_ij</span> <span class="o">*=</span> <span class="o">-</span><span class="mf">0.1</span>  <span class="c1"># Convert units/sign convention of vasp stress tensor</span>
        <span class="n">c</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">from_voigt</span><span class="p">(</span><span class="n">c_ij</span><span class="p">)</span>
        <span class="n">c</span> <span class="o">=</span> <span class="n">c</span><span class="o">.</span><span class="n">zeroed</span><span class="p">(</span><span class="n">tol</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">c</span></div></div>


<div class="viewcode-block" id="ComplianceTensor"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ComplianceTensor">[docs]</a><span class="k">class</span> <span class="nc">ComplianceTensor</span><span class="p">(</span><span class="n">Tensor</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This class represents the compliance tensor, and exists</span>
<span class="sd">    primarily to keep the voigt-conversion scheme consistent</span>
<span class="sd">    since the compliance tensor has a unique vscale</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">s_array</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            s_array ():</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">vscale</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="mi">6</span><span class="p">,</span> <span class="mi">6</span><span class="p">))</span>
        <span class="n">vscale</span><span class="p">[</span><span class="mi">3</span><span class="p">:]</span> <span class="o">*=</span> <span class="mi">2</span>
        <span class="n">vscale</span><span class="p">[:,</span> <span class="mi">3</span><span class="p">:]</span> <span class="o">*=</span> <span class="mi">2</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">s_array</span><span class="p">,</span> <span class="n">vscale</span><span class="o">=</span><span class="n">vscale</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="bp">cls</span><span class="p">)</span></div>


<div class="viewcode-block" id="ElasticTensorExpansion"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensorExpansion">[docs]</a><span class="k">class</span> <span class="nc">ElasticTensorExpansion</span><span class="p">(</span><span class="n">TensorCollection</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This class is a sequence of elastic tensors corresponding</span>
<span class="sd">    to an elastic tensor expansion, which can be used to</span>
<span class="sd">    calculate stress and energy density and inherits all</span>
<span class="sd">    of the list-based properties of TensorCollection</span>
<span class="sd">    (e. g. symmetrization, voigt conversion, etc.)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">c_list</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initialization method for ElasticTensorExpansion</span>

<span class="sd">        Args:</span>
<span class="sd">            c_list (list or tuple): sequence of Tensor inputs</span>
<span class="sd">                or tensors from which the elastic tensor</span>
<span class="sd">                expansion is constructed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">c_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">NthOrderElasticTensor</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">check_rank</span><span class="o">=</span><span class="mi">4</span> <span class="o">+</span> <span class="n">i</span> <span class="o">*</span> <span class="mi">2</span><span class="p">)</span>
                  <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">c</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">c_list</span><span class="p">)]</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">c_list</span><span class="p">)</span>

<div class="viewcode-block" id="ElasticTensorExpansion.from_diff_fit"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensorExpansion.from_diff_fit">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_diff_fit</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">strains</span><span class="p">,</span> <span class="n">stresses</span><span class="p">,</span> <span class="n">eq_stress</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                      <span class="n">tol</span><span class="o">=</span><span class="mf">1e-10</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates an elastic tensor expansion via the fitting function</span>
<span class="sd">        defined below in diff_fit</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">c_list</span> <span class="o">=</span> <span class="n">diff_fit</span><span class="p">(</span><span class="n">strains</span><span class="p">,</span> <span class="n">stresses</span><span class="p">,</span> <span class="n">eq_stress</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">tol</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">c_list</span><span class="p">)</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">order</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Order of the elastic tensor expansion, i. e. the order of the</span>
<span class="sd">        highest included set of elastic constants</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">order</span>

<div class="viewcode-block" id="ElasticTensorExpansion.calculate_stress"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensorExpansion.calculate_stress">[docs]</a>    <span class="k">def</span> <span class="nf">calculate_stress</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">strain</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculate&#39;s a given elastic tensor&#39;s contribution to the</span>
<span class="sd">        stress using Einstein summation</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">sum</span><span class="p">([</span><span class="n">c</span><span class="o">.</span><span class="n">calculate_stress</span><span class="p">(</span><span class="n">strain</span><span class="p">)</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="p">])</span></div>

<div class="viewcode-block" id="ElasticTensorExpansion.energy_density"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensorExpansion.energy_density">[docs]</a>    <span class="k">def</span> <span class="nf">energy_density</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">strain</span><span class="p">,</span> <span class="n">convert_GPa_to_eV</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates the elastic energy density due to a strain</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">sum</span><span class="p">([</span><span class="n">c</span><span class="o">.</span><span class="n">energy_density</span><span class="p">(</span><span class="n">strain</span><span class="p">,</span> <span class="n">convert_GPa_to_eV</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="p">])</span></div>

<div class="viewcode-block" id="ElasticTensorExpansion.get_ggt"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensorExpansion.get_ggt">[docs]</a>    <span class="k">def</span> <span class="nf">get_ggt</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">u</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the Generalized Gruneisen tensor for a given</span>
<span class="sd">        third-order elastic tensor expansion.</span>

<span class="sd">        Args:</span>
<span class="sd">            n (3x1 array-like): normal mode direction</span>
<span class="sd">            u (3x1 array-like): polarization direction</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">gk</span> <span class="o">=</span> <span class="bp">self</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">einsum_sequence</span><span class="p">([</span><span class="n">n</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">u</span><span class="p">])</span>
        <span class="n">result</span> <span class="o">=</span> <span class="o">-</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">gk</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">outer</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">u</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">einsum_sequence</span><span class="p">([</span><span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">])</span>
                   <span class="o">+</span> <span class="bp">self</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">einsum_sequence</span><span class="p">([</span><span class="n">n</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">u</span><span class="p">]))</span> <span class="o">/</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">gk</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">result</span></div>

<div class="viewcode-block" id="ElasticTensorExpansion.get_tgt"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensorExpansion.get_tgt">[docs]</a>    <span class="k">def</span> <span class="nf">get_tgt</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">temperature</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">structure</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">quad</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the thermodynamic Gruneisen tensor (TGT) by via an</span>
<span class="sd">        integration of the GGT weighted by the directional heat</span>
<span class="sd">        capacity.</span>

<span class="sd">        See refs:</span>
<span class="sd">            R. N. Thurston and K. Brugger, Phys. Rev. 113, A1604 (1964).</span>
<span class="sd">            K. Brugger Phys. Rev. 137, A1826 (1965).</span>

<span class="sd">        Args:</span>
<span class="sd">            temperature (float): Temperature in kelvin, if not specified</span>
<span class="sd">                will return non-cv-normalized value</span>
<span class="sd">            structure (float): Structure to be used in directional heat</span>
<span class="sd">                capacity determination, only necessary if temperature</span>
<span class="sd">                is specified</span>
<span class="sd">            quad (dict): quadrature for integration, should be</span>
<span class="sd">                dictionary with &quot;points&quot; and &quot;weights&quot; keys defaults</span>
<span class="sd">                to quadpy.sphere.Lebedev(19) as read from file</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">temperature</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">structure</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;If using temperature input, you must also &quot;</span>
                             <span class="s2">&quot;include structure&quot;</span><span class="p">)</span>

        <span class="n">quad</span> <span class="o">=</span> <span class="n">quad</span> <span class="k">if</span> <span class="n">quad</span> <span class="k">else</span> <span class="n">DEFAULT_QUAD</span>
        <span class="n">points</span> <span class="o">=</span> <span class="n">quad</span><span class="p">[</span><span class="s1">&#39;points&#39;</span><span class="p">]</span>
        <span class="n">weights</span> <span class="o">=</span> <span class="n">quad</span><span class="p">[</span><span class="s1">&#39;weights&#39;</span><span class="p">]</span>
        <span class="n">num</span><span class="p">,</span> <span class="n">denom</span><span class="p">,</span> <span class="n">c</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">)),</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span>
        <span class="k">for</span> <span class="n">p</span><span class="p">,</span> <span class="n">w</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">points</span><span class="p">,</span> <span class="n">weights</span><span class="p">):</span>
            <span class="n">gk</span> <span class="o">=</span> <span class="n">ElasticTensor</span><span class="p">(</span><span class="bp">self</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span><span class="o">.</span><span class="n">green_kristoffel</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
            <span class="n">rho_wsquareds</span><span class="p">,</span> <span class="n">us</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">eigh</span><span class="p">(</span><span class="n">gk</span><span class="p">)</span>
            <span class="n">us</span> <span class="o">=</span> <span class="p">[</span><span class="n">u</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">u</span><span class="p">)</span> <span class="k">for</span> <span class="n">u</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="n">us</span><span class="p">)]</span>
            <span class="k">for</span> <span class="n">u</span> <span class="ow">in</span> <span class="n">us</span><span class="p">:</span>
                <span class="c1"># TODO: this should be benchmarked</span>
                <span class="k">if</span> <span class="n">temperature</span><span class="p">:</span>
                    <span class="n">c</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_heat_capacity</span><span class="p">(</span><span class="n">temperature</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">p</span><span class="p">,</span> <span class="n">u</span><span class="p">)</span>
                <span class="n">num</span> <span class="o">+=</span> <span class="n">c</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_ggt</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">u</span><span class="p">)</span> <span class="o">*</span> <span class="n">w</span>
                <span class="n">denom</span> <span class="o">+=</span> <span class="n">c</span> <span class="o">*</span> <span class="n">w</span>
        <span class="k">return</span> <span class="n">SquareTensor</span><span class="p">(</span><span class="n">num</span> <span class="o">/</span> <span class="n">denom</span><span class="p">)</span></div>

<div class="viewcode-block" id="ElasticTensorExpansion.get_gruneisen_parameter"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensorExpansion.get_gruneisen_parameter">[docs]</a>    <span class="k">def</span> <span class="nf">get_gruneisen_parameter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">temperature</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">structure</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                                <span class="n">quad</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the single average gruneisen parameter from the TGT.</span>

<span class="sd">        Args:</span>
<span class="sd">            temperature (float): Temperature in kelvin, if not specified</span>
<span class="sd">                will return non-cv-normalized value</span>
<span class="sd">            structure (float): Structure to be used in directional heat</span>
<span class="sd">                capacity determination, only necessary if temperature</span>
<span class="sd">                is specified</span>
<span class="sd">            quad (dict): quadrature for integration, should be</span>
<span class="sd">                dictionary with &quot;points&quot; and &quot;weights&quot; keys defaults</span>
<span class="sd">                to quadpy.sphere.Lebedev(19) as read from file</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_tgt</span><span class="p">(</span><span class="n">temperature</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">quad</span><span class="p">))</span> <span class="o">/</span> <span class="mf">3.</span></div>

<div class="viewcode-block" id="ElasticTensorExpansion.get_heat_capacity"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensorExpansion.get_heat_capacity">[docs]</a>    <span class="k">def</span> <span class="nf">get_heat_capacity</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">temperature</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="mf">1e2</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the directional heat capacity for a higher order tensor</span>
<span class="sd">        expansion as a function of direction and polarization.</span>

<span class="sd">        Args:</span>
<span class="sd">            temperature (float): Temperature in kelvin</span>
<span class="sd">            structure (float): Structure to be used in directional heat</span>
<span class="sd">                capacity determination</span>
<span class="sd">            n (3x1 array-like): direction for Cv determination</span>
<span class="sd">            u (3x1 array-like): polarization direction, note that</span>
<span class="sd">                no attempt for verification of eigenvectors is made</span>
<span class="sd">            cutoff (float): cutoff for scale of kt / (hbar * omega)</span>
<span class="sd">                if lower than this value, returns 0</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">k</span> <span class="o">=</span> <span class="mf">1.38065e-23</span>
        <span class="n">kt</span> <span class="o">=</span> <span class="n">k</span> <span class="o">*</span> <span class="n">temperature</span>
        <span class="n">hbar_w</span> <span class="o">=</span> <span class="mf">1.05457e-34</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">omega</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">u</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">hbar_w</span> <span class="o">&gt;</span> <span class="n">kt</span> <span class="o">*</span> <span class="n">cutoff</span><span class="p">:</span>
            <span class="k">return</span> <span class="mf">0.0</span>
        <span class="n">c</span> <span class="o">=</span> <span class="n">k</span> <span class="o">*</span> <span class="p">(</span><span class="n">hbar_w</span> <span class="o">/</span> <span class="n">kt</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
        <span class="n">c</span> <span class="o">*=</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">hbar_w</span> <span class="o">/</span> <span class="n">kt</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">hbar_w</span> <span class="o">/</span> <span class="n">kt</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
        <span class="k">return</span> <span class="n">c</span> <span class="o">*</span> <span class="mf">6.022e23</span></div>

<div class="viewcode-block" id="ElasticTensorExpansion.omega"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensorExpansion.omega">[docs]</a>    <span class="k">def</span> <span class="nf">omega</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">u</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Finds directional frequency contribution to the heat</span>
<span class="sd">        capacity from direction and polarization</span>

<span class="sd">        Args:</span>
<span class="sd">            structure (Structure): Structure to be used in directional heat</span>
<span class="sd">                capacity determination</span>
<span class="sd">            n (3x1 array-like): direction for Cv determination</span>
<span class="sd">            u (3x1 array-like): polarization direction, note that</span>
<span class="sd">                no attempt for verification of eigenvectors is made</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">l0</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">matrix</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">),</span> <span class="n">n</span><span class="p">)</span>
        <span class="n">l0</span> <span class="o">*=</span> <span class="mf">1e-10</span>  <span class="c1"># in A</span>
        <span class="n">weight</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">weight</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.66054e-27</span>  <span class="c1"># in kg</span>
        <span class="n">vol</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">volume</span> <span class="o">*</span> <span class="mf">1e-30</span>  <span class="c1"># in m^3</span>
        <span class="n">vel</span> <span class="o">=</span> <span class="p">(</span><span class="mf">1e9</span> <span class="o">*</span> <span class="bp">self</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">einsum_sequence</span><span class="p">([</span><span class="n">n</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">u</span><span class="p">])</span>
               <span class="o">/</span> <span class="p">(</span><span class="n">weight</span> <span class="o">/</span> <span class="n">vol</span><span class="p">))</span> <span class="o">**</span> <span class="mf">0.5</span>
        <span class="k">return</span> <span class="n">vel</span> <span class="o">/</span> <span class="n">l0</span></div>

<div class="viewcode-block" id="ElasticTensorExpansion.thermal_expansion_coeff"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensorExpansion.thermal_expansion_coeff">[docs]</a>    <span class="k">def</span> <span class="nf">thermal_expansion_coeff</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">temperature</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s2">&quot;debye&quot;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets thermal expansion coefficient from third-order constants.</span>

<span class="sd">        Args:</span>
<span class="sd">            temperature (float): Temperature in kelvin, if not specified</span>
<span class="sd">                will return non-cv-normalized value</span>
<span class="sd">            structure (Structure): Structure to be used in directional heat</span>
<span class="sd">                capacity determination, only necessary if temperature</span>
<span class="sd">                is specified</span>
<span class="sd">            mode (string): mode for finding average heat-capacity,</span>
<span class="sd">                current supported modes are &#39;debye&#39; and &#39;dulong-petit&#39;</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">soec</span> <span class="o">=</span> <span class="n">ElasticTensor</span><span class="p">(</span><span class="bp">self</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="n">v0</span> <span class="o">=</span> <span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">volume</span> <span class="o">*</span> <span class="mf">1e-30</span> <span class="o">/</span> <span class="n">structure</span><span class="o">.</span><span class="n">num_sites</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">mode</span> <span class="o">==</span> <span class="s2">&quot;debye&quot;</span><span class="p">:</span>
            <span class="n">td</span> <span class="o">=</span> <span class="n">soec</span><span class="o">.</span><span class="n">debye_temperature</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>
            <span class="n">t_ratio</span> <span class="o">=</span> <span class="n">temperature</span> <span class="o">/</span> <span class="n">td</span>

            <span class="k">def</span> <span class="nf">integrand</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
                <span class="k">return</span> <span class="p">(</span><span class="n">x</span> <span class="o">**</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">))</span> <span class="o">/</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
            <span class="n">cv</span> <span class="o">=</span> <span class="mi">9</span> <span class="o">*</span> <span class="mf">8.314</span> <span class="o">*</span> <span class="n">t_ratio</span> <span class="o">**</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">quad</span><span class="p">(</span><span class="n">integrand</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">t_ratio</span> <span class="o">**</span> <span class="o">-</span><span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">mode</span> <span class="o">==</span> <span class="s2">&quot;dulong-petit&quot;</span><span class="p">:</span>
            <span class="n">cv</span> <span class="o">=</span> <span class="mi">3</span> <span class="o">*</span> <span class="mf">8.314</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Mode must be debye or dulong-petit&quot;</span><span class="p">)</span>
        <span class="n">tgt</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_tgt</span><span class="p">(</span><span class="n">temperature</span><span class="p">,</span> <span class="n">structure</span><span class="p">)</span>
        <span class="n">alpha</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">einsum</span><span class="p">(</span><span class="s1">&#39;ijkl,ij&#39;</span><span class="p">,</span> <span class="n">soec</span><span class="o">.</span><span class="n">compliance_tensor</span><span class="p">,</span> <span class="n">tgt</span><span class="p">)</span>
        <span class="n">alpha</span> <span class="o">*=</span> <span class="n">cv</span> <span class="o">/</span> <span class="p">(</span><span class="mf">1e9</span> <span class="o">*</span> <span class="n">v0</span> <span class="o">*</span> <span class="mf">6.022e23</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">SquareTensor</span><span class="p">(</span><span class="n">alpha</span><span class="p">)</span></div>

<div class="viewcode-block" id="ElasticTensorExpansion.get_compliance_expansion"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensorExpansion.get_compliance_expansion">[docs]</a>    <span class="k">def</span> <span class="nf">get_compliance_expansion</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets a compliance tensor expansion from the elastic</span>
<span class="sd">        tensor expansion.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># TODO: this might have a general form</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="o">&lt;=</span> <span class="mi">4</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Compliance tensor expansion only &quot;</span>
                             <span class="s2">&quot;supported for fourth-order and lower&quot;</span><span class="p">)</span>
        <span class="n">ce_exp</span> <span class="o">=</span> <span class="p">[</span><span class="n">ElasticTensor</span><span class="p">(</span><span class="bp">self</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span><span class="o">.</span><span class="n">compliance_tensor</span><span class="p">]</span>
        <span class="n">einstring</span> <span class="o">=</span> <span class="s2">&quot;ijpq,pqrsuv,rskl,uvmn-&gt;ijklmn&quot;</span>
        <span class="n">ce_exp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">einsum</span><span class="p">(</span><span class="n">einstring</span><span class="p">,</span> <span class="o">-</span><span class="n">ce_exp</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="bp">self</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span>
                                <span class="n">ce_exp</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">ce_exp</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]))</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">order</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
            <span class="c1"># Four terms in the Fourth-Order compliance tensor</span>
            <span class="n">einstring_1</span> <span class="o">=</span> <span class="s2">&quot;pqab,cdij,efkl,ghmn,abcdefgh&quot;</span>
            <span class="n">tensors_1</span> <span class="o">=</span> <span class="p">[</span><span class="n">ce_exp</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">*</span> <span class="mi">4</span> <span class="o">+</span> <span class="p">[</span><span class="bp">self</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span>
            <span class="n">temp</span> <span class="o">=</span> <span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">einsum</span><span class="p">(</span><span class="n">einstring_1</span><span class="p">,</span> <span class="o">*</span><span class="n">tensors_1</span><span class="p">)</span>
            <span class="n">einstring_2</span> <span class="o">=</span> <span class="s2">&quot;pqab,abcdef,cdijmn,efkl&quot;</span>
            <span class="n">einstring_3</span> <span class="o">=</span> <span class="s2">&quot;pqab,abcdef,efklmn,cdij&quot;</span>
            <span class="n">einstring_4</span> <span class="o">=</span> <span class="s2">&quot;pqab,abcdef,cdijkl,efmn&quot;</span>
            <span class="k">for</span> <span class="n">es</span> <span class="ow">in</span> <span class="p">[</span><span class="n">einstring_2</span><span class="p">,</span> <span class="n">einstring_3</span><span class="p">,</span> <span class="n">einstring_4</span><span class="p">]:</span>
                <span class="n">temp</span> <span class="o">-=</span> <span class="n">np</span><span class="o">.</span><span class="n">einsum</span><span class="p">(</span><span class="n">es</span><span class="p">,</span> <span class="n">ce_exp</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="bp">self</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">],</span> <span class="n">ce_exp</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">ce_exp</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="n">ce_exp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">temp</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">TensorCollection</span><span class="p">(</span><span class="n">ce_exp</span><span class="p">)</span></div>

<div class="viewcode-block" id="ElasticTensorExpansion.get_strain_from_stress"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensorExpansion.get_strain_from_stress">[docs]</a>    <span class="k">def</span> <span class="nf">get_strain_from_stress</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">stress</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the strain from a stress state according</span>
<span class="sd">        to the compliance expansion corresponding to the</span>
<span class="sd">        tensor expansion.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">compl_exp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_compliance_expansion</span><span class="p">()</span>
        <span class="n">strain</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">compl</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">compl_exp</span><span class="p">):</span>
            <span class="n">strain</span> <span class="o">+=</span> <span class="n">compl</span><span class="o">.</span><span class="n">einsum_sequence</span><span class="p">([</span><span class="n">stress</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span> <span class="o">/</span> <span class="n">factorial</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">strain</span></div>

<div class="viewcode-block" id="ElasticTensorExpansion.get_effective_ecs"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensorExpansion.get_effective_ecs">[docs]</a>    <span class="k">def</span> <span class="nf">get_effective_ecs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">strain</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="mi">2</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the effective elastic constants</span>
<span class="sd">        from the elastic tensor expansion.</span>

<span class="sd">        Args:</span>
<span class="sd">            strain (Strain or 3x3 array-like): strain condition</span>
<span class="sd">                under which to calculate the effective constants</span>
<span class="sd">            order (int): order of the ecs to be returned</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">ec_sum</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">ecs</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="p">[</span><span class="n">order</span> <span class="o">-</span> <span class="mi">2</span><span class="p">:]):</span>
            <span class="n">ec_sum</span> <span class="o">+=</span> <span class="n">ecs</span><span class="o">.</span><span class="n">einsum_sequence</span><span class="p">([</span><span class="n">strain</span><span class="p">]</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> <span class="o">/</span> <span class="n">factorial</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ec_sum</span></div>

<div class="viewcode-block" id="ElasticTensorExpansion.get_wallace_tensor"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensorExpansion.get_wallace_tensor">[docs]</a>    <span class="k">def</span> <span class="nf">get_wallace_tensor</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tau</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the Wallace Tensor for determining yield strength</span>
<span class="sd">        criteria.</span>

<span class="sd">        Args:</span>
<span class="sd">            tau (3x3 array-like): stress at which to evaluate</span>
<span class="sd">                the wallace tensor</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">b</span> <span class="o">=</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">einsum</span><span class="p">(</span><span class="s2">&quot;ml,kn-&gt;klmn&quot;</span><span class="p">,</span> <span class="n">tau</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span> <span class="o">+</span>
                   <span class="n">np</span><span class="o">.</span><span class="n">einsum</span><span class="p">(</span><span class="s2">&quot;km,ln-&gt;klmn&quot;</span><span class="p">,</span> <span class="n">tau</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span> <span class="o">+</span>
                   <span class="n">np</span><span class="o">.</span><span class="n">einsum</span><span class="p">(</span><span class="s2">&quot;nl,km-&gt;klmn&quot;</span><span class="p">,</span> <span class="n">tau</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span> <span class="o">+</span>
                   <span class="n">np</span><span class="o">.</span><span class="n">einsum</span><span class="p">(</span><span class="s2">&quot;kn,lm-&gt;klmn&quot;</span><span class="p">,</span> <span class="n">tau</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span> <span class="o">+</span>
                   <span class="o">-</span><span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">einsum</span><span class="p">(</span><span class="s2">&quot;kl,mn-&gt;klmn&quot;</span><span class="p">,</span> <span class="n">tau</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="mi">3</span><span class="p">)))</span>
        <span class="n">strain</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_strain_from_stress</span><span class="p">(</span><span class="n">tau</span><span class="p">)</span>
        <span class="n">b</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_effective_ecs</span><span class="p">(</span><span class="n">strain</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">b</span></div>

<div class="viewcode-block" id="ElasticTensorExpansion.get_symmetric_wallace_tensor"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensorExpansion.get_symmetric_wallace_tensor">[docs]</a>    <span class="k">def</span> <span class="nf">get_symmetric_wallace_tensor</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tau</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the symmetrized wallace tensor for determining</span>
<span class="sd">        yield strength criteria.</span>

<span class="sd">        Args:</span>
<span class="sd">            tau (3x3 array-like): stress at which to evaluate</span>
<span class="sd">                the wallace tensor.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">wallace</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_wallace_tensor</span><span class="p">(</span><span class="n">tau</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">Tensor</span><span class="p">(</span><span class="mf">0.5</span> <span class="o">*</span> <span class="p">(</span><span class="n">wallace</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="n">wallace</span><span class="p">,</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">])))</span></div>

<div class="viewcode-block" id="ElasticTensorExpansion.get_stability_criteria"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensorExpansion.get_stability_criteria">[docs]</a>    <span class="k">def</span> <span class="nf">get_stability_criteria</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the stability criteria from the symmetric</span>
<span class="sd">        Wallace tensor from an input vector and stress</span>
<span class="sd">        value.</span>

<span class="sd">        Args:</span>
<span class="sd">            s (float): Stress value at which to evaluate</span>
<span class="sd">                the stability criteria</span>
<span class="sd">            n (3x1 array-like): direction of the applied</span>
<span class="sd">                stress</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">n</span> <span class="o">=</span> <span class="n">get_uvec</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
        <span class="n">stress</span> <span class="o">=</span> <span class="n">s</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">outer</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
        <span class="n">sym_wallace</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_symmetric_wallace_tensor</span><span class="p">(</span><span class="n">stress</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">det</span><span class="p">(</span><span class="n">sym_wallace</span><span class="o">.</span><span class="n">voigt</span><span class="p">)</span></div>

<div class="viewcode-block" id="ElasticTensorExpansion.get_yield_stress"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.ElasticTensorExpansion.get_yield_stress">[docs]</a>    <span class="k">def</span> <span class="nf">get_yield_stress</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the yield stress for a given direction</span>

<span class="sd">        Args:</span>
<span class="sd">            n (3x1 array-like): direction for which to find the</span>
<span class="sd">                yield stress</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># TODO: root finding could be more robust</span>
        <span class="n">comp</span> <span class="o">=</span> <span class="n">root</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_stability_criteria</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="n">n</span><span class="p">)</span>
        <span class="n">tens</span> <span class="o">=</span> <span class="n">root</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_stability_criteria</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="n">n</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">comp</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="n">tens</span><span class="o">.</span><span class="n">x</span><span class="p">)</span></div></div>


<span class="c1"># TODO: abstract this for other tensor fitting procedures</span>
<div class="viewcode-block" id="diff_fit"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.diff_fit">[docs]</a><span class="k">def</span> <span class="nf">diff_fit</span><span class="p">(</span><span class="n">strains</span><span class="p">,</span> <span class="n">stresses</span><span class="p">,</span> <span class="n">eq_stress</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">1e-10</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    nth order elastic constant fitting function based on</span>
<span class="sd">    central-difference derivatives with respect to distinct</span>
<span class="sd">    strain states.  The algorithm is summarized as follows:</span>

<span class="sd">    1. Identify distinct strain states as sets of indices</span>
<span class="sd">       for which nonzero strain values exist, typically</span>
<span class="sd">       [(0), (1), (2), (3), (4), (5), (0, 1) etc.]</span>
<span class="sd">    2. For each strain state, find and sort strains and</span>
<span class="sd">       stresses by strain value.</span>
<span class="sd">    3. Find first, second .. nth derivatives of each stress</span>
<span class="sd">       with respect to scalar variable corresponding to</span>
<span class="sd">       the smallest perturbation in the strain.</span>
<span class="sd">    4. Use the pseudoinverse of a matrix-vector expression</span>
<span class="sd">       corresponding to the parameterized stress-strain</span>
<span class="sd">       relationship and multiply that matrix by the respective</span>
<span class="sd">       calculated first or second derivatives from the</span>
<span class="sd">       previous step.</span>
<span class="sd">    5. Place the calculated nth-order elastic</span>
<span class="sd">       constants appropriately.</span>

<span class="sd">    Args:</span>
<span class="sd">        order (int): order of the elastic tensor set to return</span>
<span class="sd">        strains (nx3x3 array-like): Array of 3x3 strains</span>
<span class="sd">            to use in fitting of ECs</span>
<span class="sd">        stresses (nx3x3 array-like): Array of 3x3 stresses</span>
<span class="sd">            to use in fitting ECs.  These should be PK2 stresses.</span>
<span class="sd">        eq_stress (3x3 array-like): stress corresponding to</span>
<span class="sd">            equilibrium strain (i. e. &quot;0&quot; strain state).</span>
<span class="sd">            If not specified, function will try to find</span>
<span class="sd">            the state in the list of provided stresses</span>
<span class="sd">            and strains.  If not found, defaults to 0.</span>
<span class="sd">        tol (float): value for which strains below</span>
<span class="sd">            are ignored in identifying strain states.</span>

<span class="sd">    Returns:</span>
<span class="sd">        Set of tensors corresponding to nth order expansion of</span>
<span class="sd">        the stress/strain relation</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">strain_state_dict</span> <span class="o">=</span> <span class="n">get_strain_state_dict</span><span class="p">(</span>
        <span class="n">strains</span><span class="p">,</span> <span class="n">stresses</span><span class="p">,</span> <span class="n">eq_stress</span><span class="o">=</span><span class="n">eq_stress</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="n">tol</span><span class="p">,</span>
        <span class="n">add_eq</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">sort</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="c1"># Collect derivative data</span>
    <span class="n">c_list</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">dei_dsi</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">order</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">strain_state_dict</span><span class="p">)))</span>
    <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="p">(</span><span class="n">strain_state</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">strain_state_dict</span><span class="o">.</span><span class="n">items</span><span class="p">()):</span>
        <span class="n">hvec</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s2">&quot;strains&quot;</span><span class="p">][:,</span> <span class="n">strain_state</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="mi">1</span><span class="p">)]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">order</span><span class="p">):</span>
            <span class="n">coef</span> <span class="o">=</span> <span class="n">get_diff_coeff</span><span class="p">(</span><span class="n">hvec</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
            <span class="n">dei_dsi</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="p">:,</span> <span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">coef</span><span class="p">,</span> <span class="n">data</span><span class="p">[</span><span class="s2">&quot;stresses&quot;</span><span class="p">])</span>

    <span class="n">m</span><span class="p">,</span> <span class="n">absent</span> <span class="o">=</span> <span class="n">generate_pseudo</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">strain_state_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">()),</span> <span class="n">order</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">order</span><span class="p">):</span>
        <span class="n">cvec</span><span class="p">,</span> <span class="n">carr</span> <span class="o">=</span> <span class="n">get_symbol_list</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">svec</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">dei_dsi</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">T</span><span class="p">)</span>
        <span class="n">cmap</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">cvec</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">],</span> <span class="n">svec</span><span class="p">)))</span>
        <span class="n">c_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">v_subs</span><span class="p">(</span><span class="n">carr</span><span class="p">,</span> <span class="n">cmap</span><span class="p">))</span>
    <span class="k">return</span> <span class="p">[</span><span class="n">Tensor</span><span class="o">.</span><span class="n">from_voigt</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">c_list</span><span class="p">]</span></div>


<div class="viewcode-block" id="find_eq_stress"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.find_eq_stress">[docs]</a><span class="k">def</span> <span class="nf">find_eq_stress</span><span class="p">(</span><span class="n">strains</span><span class="p">,</span> <span class="n">stresses</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">1e-10</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Finds stress corresponding to zero strain state in stress-strain list</span>

<span class="sd">    Args:</span>
<span class="sd">        strains (Nx3x3 array-like): array corresponding to strains</span>
<span class="sd">        stresses (Nx3x3 array-like): array corresponding to stresses</span>
<span class="sd">        tol (float): tolerance to find zero strain state</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">stress_array</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">stresses</span><span class="p">)</span>
    <span class="n">strain_array</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">strains</span><span class="p">)</span>
    <span class="n">eq_stress</span> <span class="o">=</span> <span class="n">stress_array</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">strain_array</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">tol</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">))]</span>

    <span class="k">if</span> <span class="n">eq_stress</span><span class="o">.</span><span class="n">size</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">all_same</span> <span class="o">=</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">eq_stress</span> <span class="o">-</span> <span class="n">eq_stress</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">&lt;</span> <span class="mf">1e-8</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">eq_stress</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">all_same</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Multiple stresses found for equilibrium strain&quot;</span>
                             <span class="s2">&quot; state, please specify equilibrium stress or  &quot;</span>
                             <span class="s2">&quot; remove extraneous stresses.&quot;</span><span class="p">)</span>
        <span class="n">eq_stress</span> <span class="o">=</span> <span class="n">eq_stress</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;No eq state found, returning zero voigt stress&quot;</span><span class="p">)</span>
        <span class="n">eq_stress</span> <span class="o">=</span> <span class="n">Stress</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">)))</span>
    <span class="k">return</span> <span class="n">eq_stress</span></div>


<div class="viewcode-block" id="get_strain_state_dict"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.get_strain_state_dict">[docs]</a><span class="k">def</span> <span class="nf">get_strain_state_dict</span><span class="p">(</span><span class="n">strains</span><span class="p">,</span> <span class="n">stresses</span><span class="p">,</span> <span class="n">eq_stress</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                          <span class="n">tol</span><span class="o">=</span><span class="mf">1e-10</span><span class="p">,</span> <span class="n">add_eq</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">sort</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Creates a dictionary of voigt-notation stress-strain sets</span>
<span class="sd">    keyed by &quot;strain state&quot;, i. e. a tuple corresponding to</span>
<span class="sd">    the non-zero entries in ratios to the lowest nonzero value,</span>
<span class="sd">    e.g. [0, 0.1, 0, 0.2, 0, 0] -&gt; (0,1,0,2,0,0)</span>
<span class="sd">    This allows strains to be collected in stencils as to</span>
<span class="sd">    evaluate parameterized finite difference derivatives</span>

<span class="sd">    Args:</span>
<span class="sd">        strains (Nx3x3 array-like): strain matrices</span>
<span class="sd">        stresses (Nx3x3 array-like): stress matrices</span>
<span class="sd">        eq_stress (Nx3x3 array-like): equilibrium stress</span>
<span class="sd">        tol (float): tolerance for sorting strain states</span>
<span class="sd">        add_eq (bool): flag for whether to add eq_strain</span>
<span class="sd">            to stress-strain sets for each strain state</span>
<span class="sd">        sort (bool): flag for whether to sort strain states</span>

<span class="sd">    Returns:</span>
<span class="sd">        OrderedDict with strain state keys and dictionaries</span>
<span class="sd">        with stress-strain data corresponding to strain state</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># Recast stress/strains</span>
    <span class="n">vstrains</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">Strain</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="o">.</span><span class="n">zeroed</span><span class="p">(</span><span class="n">tol</span><span class="p">)</span><span class="o">.</span><span class="n">voigt</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">strains</span><span class="p">])</span>
    <span class="n">vstresses</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">Stress</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="o">.</span><span class="n">zeroed</span><span class="p">(</span><span class="n">tol</span><span class="p">)</span><span class="o">.</span><span class="n">voigt</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">stresses</span><span class="p">])</span>
    <span class="c1"># Collect independent strain states:</span>
    <span class="n">independent</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="nb">tuple</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nonzero</span><span class="p">(</span><span class="n">vstrain</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">tolist</span><span class="p">())</span>
                       <span class="k">for</span> <span class="n">vstrain</span> <span class="ow">in</span> <span class="n">vstrains</span><span class="p">])</span>
    <span class="n">strain_state_dict</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">add_eq</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">eq_stress</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">veq_stress</span> <span class="o">=</span> <span class="n">Stress</span><span class="p">(</span><span class="n">eq_stress</span><span class="p">)</span><span class="o">.</span><span class="n">voigt</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">veq_stress</span> <span class="o">=</span> <span class="n">find_eq_stress</span><span class="p">(</span><span class="n">strains</span><span class="p">,</span> <span class="n">stresses</span><span class="p">)</span><span class="o">.</span><span class="n">voigt</span>

    <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">ind</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">independent</span><span class="p">):</span>
        <span class="c1"># match strains with templates</span>
        <span class="n">template</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">bool</span><span class="p">)</span>
        <span class="n">np</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">template</span><span class="p">,</span> <span class="n">ind</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
        <span class="n">template</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">tile</span><span class="p">(</span><span class="n">template</span><span class="p">,</span> <span class="p">[</span><span class="n">vstresses</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="mi">1</span><span class="p">])</span>
        <span class="n">mode</span> <span class="o">=</span> <span class="p">(</span><span class="n">template</span> <span class="o">==</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">vstrains</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mf">1e-10</span><span class="p">))</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">mstresses</span> <span class="o">=</span> <span class="n">vstresses</span><span class="p">[</span><span class="n">mode</span><span class="p">]</span>
        <span class="n">mstrains</span> <span class="o">=</span> <span class="n">vstrains</span><span class="p">[</span><span class="n">mode</span><span class="p">]</span>
        <span class="c1"># Get &quot;strain state&quot;, i.e. ratio of each value to minimum strain</span>
        <span class="n">min_nonzero_ind</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmin</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">take</span><span class="p">(</span><span class="n">mstrains</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">ind</span><span class="p">)))</span>
        <span class="n">min_nonzero_val</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">take</span><span class="p">(</span><span class="n">mstrains</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">ind</span><span class="p">)[</span><span class="n">min_nonzero_ind</span><span class="p">]</span>
        <span class="n">strain_state</span> <span class="o">=</span> <span class="n">mstrains</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">/</span> <span class="n">min_nonzero_val</span>
        <span class="n">strain_state</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">strain_state</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">add_eq</span><span class="p">:</span>
            <span class="c1"># add zero strain state</span>
            <span class="n">mstrains</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">([</span><span class="n">mstrains</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">6</span><span class="p">)])</span>
            <span class="n">mstresses</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">([</span><span class="n">mstresses</span><span class="p">,</span> <span class="n">veq_stress</span><span class="p">])</span>
        <span class="c1"># sort strains/stresses by strain values</span>
        <span class="k">if</span> <span class="n">sort</span><span class="p">:</span>
            <span class="n">mstresses</span> <span class="o">=</span> <span class="n">mstresses</span><span class="p">[</span><span class="n">mstrains</span><span class="p">[:,</span> <span class="n">ind</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span><span class="o">.</span><span class="n">argsort</span><span class="p">()]</span>
            <span class="n">mstrains</span> <span class="o">=</span> <span class="n">mstrains</span><span class="p">[</span><span class="n">mstrains</span><span class="p">[:,</span> <span class="n">ind</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span><span class="o">.</span><span class="n">argsort</span><span class="p">()]</span>
        <span class="n">strain_state_dict</span><span class="p">[</span><span class="n">strain_state</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;strains&quot;</span><span class="p">:</span> <span class="n">mstrains</span><span class="p">,</span>
                                           <span class="s2">&quot;stresses&quot;</span><span class="p">:</span> <span class="n">mstresses</span><span class="p">}</span>
    <span class="k">return</span> <span class="n">strain_state_dict</span></div>


<div class="viewcode-block" id="generate_pseudo"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.generate_pseudo">[docs]</a><span class="k">def</span> <span class="nf">generate_pseudo</span><span class="p">(</span><span class="n">strain_states</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates the pseudoinverse for a given set of strains.</span>

<span class="sd">    Args:</span>
<span class="sd">        strain_states (6xN array like): a list of voigt-notation</span>
<span class="sd">            &quot;strain-states&quot;, i. e. perturbed indices of the strain</span>
<span class="sd">            as a function of the smallest strain e. g. (0, 1, 0, 0, 1, 0)</span>
<span class="sd">        order (int): order of pseudoinverse to calculate</span>

<span class="sd">    Returns:</span>
<span class="sd">        mis: pseudo inverses for each order tensor, these can</span>
<span class="sd">            be multiplied by the central difference derivative</span>
<span class="sd">            of the stress with respect to the strain state</span>
<span class="sd">        absent_syms: symbols of the tensor absent from the PI</span>
<span class="sd">            expression</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">sp</span><span class="o">.</span><span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;s&#39;</span><span class="p">)</span>
    <span class="n">nstates</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">strain_states</span><span class="p">)</span>
    <span class="n">ni</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">strain_states</span><span class="p">)</span> <span class="o">*</span> <span class="n">s</span>
    <span class="n">mis</span><span class="p">,</span> <span class="n">absent_syms</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">degree</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">order</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
        <span class="n">cvec</span><span class="p">,</span> <span class="n">carr</span> <span class="o">=</span> <span class="n">get_symbol_list</span><span class="p">(</span><span class="n">degree</span><span class="p">)</span>
        <span class="n">sarr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">nstates</span><span class="p">,</span> <span class="mi">6</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">object</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">strain_v</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">ni</span><span class="p">):</span>
            <span class="c1"># Get expressions</span>
            <span class="n">exps</span> <span class="o">=</span> <span class="n">carr</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">degree</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
                <span class="n">exps</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">exps</span><span class="p">,</span> <span class="n">strain_v</span><span class="p">)</span>
            <span class="n">exps</span> <span class="o">/=</span> <span class="n">np</span><span class="o">.</span><span class="n">math</span><span class="o">.</span><span class="n">factorial</span><span class="p">(</span><span class="n">degree</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
            <span class="n">sarr</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">sp</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">exp</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">degree</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="k">for</span> <span class="n">exp</span> <span class="ow">in</span> <span class="n">exps</span><span class="p">]</span>
        <span class="n">svec</span> <span class="o">=</span> <span class="n">sarr</span><span class="o">.</span><span class="n">ravel</span><span class="p">()</span>
        <span class="n">present_syms</span> <span class="o">=</span> <span class="nb">set</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="n">exp</span><span class="o">.</span><span class="n">atoms</span><span class="p">(</span><span class="n">sp</span><span class="o">.</span><span class="n">Symbol</span><span class="p">)</span> <span class="k">for</span> <span class="n">exp</span> <span class="ow">in</span> <span class="n">svec</span><span class="p">])</span>
        <span class="n">absent_syms</span> <span class="o">+=</span> <span class="p">[</span><span class="nb">set</span><span class="p">(</span><span class="n">cvec</span><span class="p">)</span> <span class="o">-</span> <span class="n">present_syms</span><span class="p">]</span>
        <span class="n">m</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">6</span> <span class="o">*</span> <span class="n">nstates</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">cvec</span><span class="p">)))</span>
        <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">c</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">cvec</span><span class="p">):</span>
            <span class="n">m</span><span class="p">[:,</span> <span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="n">v_diff</span><span class="p">(</span><span class="n">svec</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span>
        <span class="n">mis</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">pinv</span><span class="p">(</span><span class="n">m</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">mis</span><span class="p">,</span> <span class="n">absent_syms</span></div>


<div class="viewcode-block" id="get_symbol_list"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.get_symbol_list">[docs]</a><span class="k">def</span> <span class="nf">get_symbol_list</span><span class="p">(</span><span class="n">rank</span><span class="p">,</span> <span class="n">dim</span><span class="o">=</span><span class="mi">6</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns a symbolic representation of the voigt-notation</span>
<span class="sd">    tensor that places identical symbols for entries related</span>
<span class="sd">    by index transposition, i. e. C_1121 = C_1211 etc.</span>

<span class="sd">    Args:</span>
<span class="sd">        dim (int): dimension of matrix/tensor, e. g. 6 for</span>
<span class="sd">            voigt notation and 3 for standard</span>
<span class="sd">        rank (int): rank of tensor, e. g. 3 for third-order ECs</span>

<span class="sd">    Returns:</span>
<span class="sd">        c_vec (array): array representing distinct indices</span>
<span class="sd">        c_arr (array): array representing tensor with equivalent</span>
<span class="sd">            indices assigned as above</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">indices</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span>
        <span class="n">itertools</span><span class="o">.</span><span class="n">combinations_with_replacement</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">dim</span><span class="p">),</span> <span class="n">r</span><span class="o">=</span><span class="n">rank</span><span class="p">))</span>
    <span class="n">c_vec</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">indices</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">object</span><span class="p">)</span>
    <span class="n">c_arr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">([</span><span class="n">dim</span><span class="p">]</span> <span class="o">*</span> <span class="n">rank</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">object</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">indices</span><span class="p">):</span>
        <span class="n">c_vec</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="n">sp</span><span class="o">.</span><span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;c_&#39;</span> <span class="o">+</span> <span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">idx</span><span class="p">]))</span>
        <span class="k">for</span> <span class="n">perm</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">permutations</span><span class="p">(</span><span class="n">idx</span><span class="p">):</span>
            <span class="n">c_arr</span><span class="p">[</span><span class="n">perm</span><span class="p">]</span> <span class="o">=</span> <span class="n">c_vec</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>
    <span class="k">return</span> <span class="n">c_vec</span><span class="p">,</span> <span class="n">c_arr</span></div>


<div class="viewcode-block" id="subs"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.subs">[docs]</a><span class="k">def</span> <span class="nf">subs</span><span class="p">(</span><span class="n">entry</span><span class="p">,</span> <span class="n">cmap</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Sympy substitution function, primarily for the purposes</span>
<span class="sd">    of numpy vectorization</span>

<span class="sd">    Args:</span>
<span class="sd">        entry (symbol or exp): sympy expr to undergo subs</span>
<span class="sd">        cmap (dict): map for symbols to values to use in subs</span>

<span class="sd">    Returns:</span>
<span class="sd">        Evaluated expression with substitution</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">entry</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">cmap</span><span class="p">)</span></div>


<span class="c1"># Vectorized functions</span>
<span class="n">v_subs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vectorize</span><span class="p">(</span><span class="n">subs</span><span class="p">)</span>
<span class="n">v_diff</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vectorize</span><span class="p">(</span><span class="n">sp</span><span class="o">.</span><span class="n">diff</span><span class="p">)</span>


<div class="viewcode-block" id="get_diff_coeff"><a class="viewcode-back" href="../../../../pymatgen.analysis.elasticity.elastic.html#pymatgen.analysis.elasticity.elastic.get_diff_coeff">[docs]</a><span class="k">def</span> <span class="nf">get_diff_coeff</span><span class="p">(</span><span class="n">hvec</span><span class="p">,</span> <span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Helper function to find difference coefficients of an</span>
<span class="sd">    derivative on an arbitrary mesh.</span>

<span class="sd">    Args:</span>
<span class="sd">        hvec (1D array-like): sampling stencil</span>
<span class="sd">        n (int): degree of derivative to find</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">hvec</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">hvec</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float</span><span class="p">)</span>
    <span class="n">acc</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">hvec</span><span class="p">)</span>
    <span class="n">exp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">column_stack</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">acc</span><span class="p">)]</span> <span class="o">*</span> <span class="n">acc</span><span class="p">)</span>
    <span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">([</span><span class="n">hvec</span><span class="p">]</span> <span class="o">*</span> <span class="n">acc</span><span class="p">)</span> <span class="o">**</span> <span class="n">exp</span>
    <span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">acc</span><span class="p">)</span>
    <span class="n">b</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="n">factorial</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">solve</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.elasticity.elastic</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>